package com.ruoyi.system.service.impl;

import com.ruoyi.common.snmp.getSnmp.SNMPSessionUtil;
import com.ruoyi.common.snmp.getSnmpf.GetInformation;
import com.ruoyi.system.domain.SnmpCpu;
import com.ruoyi.system.mapper.SnmpCpuMapper;
import com.ruoyi.system.service.ISnmpCpuService;
import org.snmp4j.PDU;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@Service
public class SnmpCpuServiceImpl implements ISnmpCpuService
{
    @Autowired
    private SnmpCpuMapper snmpCpuMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SnmpCpu selectSnmpCpuById(Long id)
    {
        return snmpCpuMapper.selectSnmpCpuById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param snmpCpu 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SnmpCpu> selectSnmpCpuList(SnmpCpu snmpCpu)
    {
        return snmpCpuMapper.selectSnmpCpuList(snmpCpu);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param snmpCpu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSnmpCpu(SnmpCpu snmpCpu)
    {
        return snmpCpuMapper.insertSnmpCpu(snmpCpu);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param snmpCpu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSnmpCpu(SnmpCpu snmpCpu)
    {
        return snmpCpuMapper.updateSnmpCpu(snmpCpu);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpCpuByIds(Long[] ids)
    {
        return snmpCpuMapper.deleteSnmpCpuByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSnmpCpuById(Long id)
    {
        return snmpCpuMapper.deleteSnmpCpuById(id);
    }

    @Override
    public List<SnmpCpu>  GetCpuInfoOne(String ip) {
        return snmpCpuMapper.GetCpuInfoOne(ip);
    }
    public List<SnmpCpu> GetInfo(String ip) throws Exception {

        List<SnmpCpu> arry=new ArrayList<>();
        arry.add(GetCpuInfo(ip));
        return arry;
    }
    public SnmpCpu GetCpuInfo(String ip) throws Exception {
        SNMPSessionUtil sessionUtil = new SNMPSessionUtil(ip, "161", "public", "2");
        String[] isSnmp = {".1.3.6.1.2.1.1.3"};
        ArrayList<String> isSnmpGet = sessionUtil.getIsSnmpGet(PDU.GET, isSnmp);
        String[] oids = {".1.3.6.1.2.1.25.3.3.1.2"};
        String[] oids2 = {".1.3.6.1.2.1.4.1.0"};
        String[] ciscoCpu = {".1.3.6.1.4.1.9.2.1.58"};
        String[] ruijieCpu = {".1.3.6.1.4.1.4881.1.1.10.2.36.1.1.3.0"};
        String[] oids3 = {".1.3.6.1.2.1.1.1.0"};
        ArrayList<String> type = sessionUtil.getSnmpGet(PDU.GET, oids3);
        ArrayList<String> dtype = sessionUtil.snmpWalk2(oids);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<String> forwording = sessionUtil.getSnmpGet(PDU.GET, oids2);
        if ("-1".equals(isSnmpGet.get(0))) {
            SnmpCpu c = new SnmpCpu();
            c.setIp(ip);
            c.setCpuRate(0.0);
            c.setTime(new Date());
            snmpCpuMapper.insertSnmpCpu(c);
            return c;
        } else {
            if (!dtype.isEmpty() && ("2".equals(forwording.get(0))||"1".equals(forwording.get(0)))) {
                SnmpCpu CpuInfo = new SnmpCpu();
                CpuInfo.setIp(ip);
                List<TableEvent> list = GetInformation.getCpuInfo(ip);
                if (list.size() == 1 && list.get(0).getColumns() == null) {
                    throw new Exception("未获取到信息！");
                } else {
                    int percentage = 0;
                    for (TableEvent event : list) {
                        VariableBinding[] values = event.getColumns();
                        System.out.println(values[0].getVariable());
                        percentage += Integer.parseInt(values[0].getVariable().toString());
                    }
                    CpuInfo.setCpuRate((double) (percentage / list.size()));
                }
                CpuInfo.setTime(new Date());
                snmpCpuMapper.insertSnmpCpu(CpuInfo);
                return CpuInfo;
            } else if (type.get(0).contains("Cisco")) {// cisco设备
                SnmpCpu cisco = new SnmpCpu();
                Integer cp = 0;
                ArrayList<String> cpu = sessionUtil.snmpWalk2(ciscoCpu); //五分钟平均利用率
                List<Integer> c = new ArrayList<>();
                for (String s : cpu) {
                    c.add(Integer.valueOf(s.substring(s.lastIndexOf("=")).replace("=", "").trim()));
                }
                for (Integer integer : c) {
                    cp += integer;
                }
                cisco.setIp(ip);
                if (c.size()==0){
                    cisco.setCpuRate((double) cp );
                }else {
                    cisco.setCpuRate((double) cp/c.size() );
                }
                cisco.setTime(new Date());
                snmpCpuMapper.insertSnmpCpu(cisco);
                return cisco;
            } else if (type.get(0).contains("Ruijie")) {
                SnmpCpu ruijie = new SnmpCpu();
                ArrayList<String> cpu = sessionUtil.getSnmpGet(PDU.GET, ruijieCpu); //五分钟平均利用率
                ruijie.setIp(ip);
                ruijie.setCpuRate(Double.parseDouble(cpu.get(0)));
                ruijie.setTime(new Date());
                snmpCpuMapper.insertSnmpCpu(ruijie);
                return ruijie;
            }else if (type.get(0).contains("Xiaomi") || type.get(0).contains("ImmortalWrt")) {
                // 假设这是针对小米或ImmortalWrt路由器获取CPU利用率的正确OID
                String[] cpuOids = {"1.3.6.1.2.1.25.3.3.1.2"}; // HOST-RESOURCES-MIB::hrProcessorLoad

                SnmpCpu xiaomi = new SnmpCpu();
                ArrayList<String> cpuValues = sessionUtil.snmpWalk2(cpuOids); // 使用snmpWalk2来获取所有CPU负载条目

                Integer cp = 0;
                List<Integer> cpuLoads = new ArrayList<>();

                for (String s : cpuValues) {
                    cpuLoads.add(Integer.valueOf(s.substring(s.lastIndexOf("=") + 1).trim()));
                }

                for (Integer load : cpuLoads) {
                    cp += load;
                }

                xiaomi.setIp(ip);
                if (cpuLoads.size() == 0) {
                    xiaomi.setCpuRate((double) cp);
                } else {
                    // 计算平均CPU利用率
                    double averageCpuRate = (double) cp / cpuLoads.size();
                    xiaomi.setCpuRate(averageCpuRate);
                }

                xiaomi.setTime(new Date());
                snmpCpuMapper.insertSnmpCpu(xiaomi);
                return xiaomi;
            }
        }
        SnmpCpu c = new SnmpCpu();
        c.setIp(ip);
        c.setCpuRate(0.0);
        c.setTime(new Date());
        return c;
    }
}
